using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Markup;
using System.Windows.Media;
using Common;
using Common.Enum;
using Common.NotifyEvent;
using CustomChart;
using Infragistics.UltraChart.Data.Series;
using Infragistics.UltraChart.Resources.Appearance;
using SExperiment;
using SProject.Paramter;
using SSettings;
using SWellPlate;

namespace MainView.MainControls.Chart
{
	public abstract partial class BaseCurveChart : System.Windows.Controls.UserControl, IDisposable, IComponentConnector
	{
		public static readonly RoutedEvent OnItemUpdateRoutedEvent = EventManager.RegisterRoutedEvent("OnItemUpdateRoutedEvent", RoutingStrategy.Bubble, typeof(EventHandler<RoutedEventArgs>), typeof(BaseCurveChart));

		public static readonly RoutedEvent OnLineColorChangedRoutedEvent = EventManager.RegisterRoutedEvent("OnLineColorChangedRoutedEvent", RoutingStrategy.Bubble, typeof(EventHandler<RoutedEventArgs>), typeof(BaseCurveChart));

		public static readonly RoutedEvent OnThresholdMouseUpRpoutedEvent = EventManager.RegisterRoutedEvent("OnThresholdMouseUpRpoutedEvent", RoutingStrategy.Bubble, typeof(EventHandler<RoutedEventArgs>), typeof(BaseCurveChart));

		private NormalChart _Chart;

		public static readonly DependencyProperty PrintModeProperty = DependencyProperty.Register("PrintMode", typeof(bool), typeof(BaseCurveChart), new UIPropertyMetadata(false));

		public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register("Caption", typeof(string), typeof(BaseCurveChart), new UIPropertyMetadata(null, OnCaptionChanged));

		public static readonly DependencyProperty HighLightProperty = DependencyProperty.Register("HighLight", typeof(bool), typeof(BaseCurveChart), new UIPropertyMetadata(true));

		public static readonly DependencyProperty ColorModeProperty = DependencyProperty.Register("ColorMode", typeof(EColorMode), typeof(BaseCurveChart), new UIPropertyMetadata(EColorMode.Channel, OnColorModeChanged));

		public static readonly DependencyProperty TitleXFormatProperty = DependencyProperty.Register("TitleXFormat", typeof(string), typeof(BaseCurveChart), new UIPropertyMetadata(null, OnTitleXFormatChanged));

		public static readonly DependencyProperty TitleYFormatProperty = DependencyProperty.Register("TitleYFormat", typeof(string), typeof(BaseCurveChart), new UIPropertyMetadata(null, OnTitleYFormatChanged));

		public static readonly DependencyProperty TitleXProperty = DependencyProperty.Register("TitleX", typeof(string), typeof(BaseCurveChart), new UIPropertyMetadata(null, OnTitleXChanged));

		public static readonly DependencyProperty TitleYProperty = DependencyProperty.Register("TitleY", typeof(string), typeof(BaseCurveChart), new UIPropertyMetadata(null, OnTitleYChanged));

		public static readonly DependencyProperty ThresholdVisibleProperty = DependencyProperty.Register("ThresholdVisible", typeof(bool), typeof(BaseCurveChart), new UIPropertyMetadata(false, OnThresholdVisibleChanged));

		public static readonly DependencyProperty SubsetProperty = DependencyProperty.Register("Subset", typeof(Subset), typeof(BaseCurveChart), new UIPropertyMetadata(null));

		public static readonly DependencyProperty MaxXProperty = DependencyProperty.Register("MaxX", typeof(double), typeof(BaseCurveChart), new PropertyMetadata(1.0, OnMaxXChanged));

		public static readonly DependencyProperty MinXProperty = DependencyProperty.Register("MinX", typeof(double), typeof(BaseCurveChart), new PropertyMetadata(0.0, OnMinXChanged));

		public static readonly DependencyProperty MaxYProperty = DependencyProperty.Register("MaxY", typeof(double), typeof(BaseCurveChart), new PropertyMetadata(10.0, OnMaxYChanged));

		public static readonly DependencyProperty MinYProperty = DependencyProperty.Register("MinY", typeof(double), typeof(BaseCurveChart), new PropertyMetadata(-10.0, OnMinYChanged));

		public static readonly DependencyProperty IntervalProperty = DependencyProperty.Register("Interval", typeof(int), typeof(BaseCurveChart), new UIPropertyMetadata(1));

		public bool IsPcrResult;

		public static readonly DependencyProperty TargetsProperty = DependencyProperty.Register("Targets", typeof(IList<ChartData>), typeof(BaseCurveChart), new UIPropertyMetadata(null, OnTargetsChanged));

		public static readonly DependencyProperty CurverWidthProperty = DependencyProperty.Register("CurverWidth", typeof(ESizeMode), typeof(BaseCurveChart), new UIPropertyMetadata(ESizeMode.Middle, OnCurverWidthChanged));

		public static readonly DependencyProperty IsDecryptProperty = DependencyProperty.Register("IsDecrypt", typeof(bool), typeof(BaseCurveChart), new UIPropertyMetadata(true, OnIsDecryptPropertyChanged));

		public static readonly DependencyProperty ShowCurveToolTipsProperty = DependencyProperty.Register("ShowCurveToolTips", typeof(bool), typeof(BaseCurveChart), new UIPropertyMetadata(true, OnShowCurveToolTipsChanged));

		protected List<XYSeriesEx> _ThresholdSeriesList = new List<XYSeriesEx>();

		protected Hashtable _DisplaySeries = new Hashtable();

		protected List<ChartData> _DisplayChartData = new List<ChartData>();

		private Hashtable htSubsetParamterEvent = new Hashtable();

		private bool _IsMouseDown;

		private XYSeriesEx _ThresholdSeries;

		private bool _RefreshAll = true;

		private int digitLen = 1;

		public NormalChart UltraChart
		{
			get
			{
				if (windowsFormsHost.Child == null)
				{
					_Chart = new NormalChart();
					_Chart.Chart.Dock = DockStyle.Fill;
					windowsFormsHost.Child = _Chart.Chart;
				}
				return _Chart;
			}
		}

		public bool PrintMode
		{
			get
			{
				return (bool)GetValue(PrintModeProperty);
			}
			set
			{
				SetValue(PrintModeProperty, value);
			}
		}

		public string Caption
		{
			get
			{
				return (string)GetValue(CaptionProperty);
			}
			set
			{
				SetValue(CaptionProperty, value);
			}
		}

		public bool HighLight
		{
			get
			{
				return (bool)GetValue(HighLightProperty);
			}
			set
			{
				SetValue(HighLightProperty, value);
			}
		}

		public EColorMode ColorMode
		{
			get
			{
				return (EColorMode)GetValue(ColorModeProperty);
			}
			set
			{
				SetValue(ColorModeProperty, value);
			}
		}

		public string TitleXFormat
		{
			get
			{
				return (string)GetValue(TitleXFormatProperty);
			}
			set
			{
				SetValue(TitleXFormatProperty, value);
			}
		}

		public string TitleYFormat
		{
			get
			{
				return (string)GetValue(TitleYFormatProperty);
			}
			set
			{
				SetValue(TitleYFormatProperty, value);
			}
		}

		public string TitleX
		{
			get
			{
				return (string)GetValue(TitleXProperty);
			}
			set
			{
				SetValue(TitleXProperty, value);
			}
		}

		public string TitleY
		{
			get
			{
				return (string)GetValue(TitleYProperty);
			}
			set
			{
				SetValue(TitleYProperty, value);
			}
		}

		public bool ThresholdVisible
		{
			get
			{
				return (bool)GetValue(ThresholdVisibleProperty);
			}
			set
			{
				SetValue(ThresholdVisibleProperty, value);
			}
		}

		public Subset Subset
		{
			get
			{
				return (Subset)GetValue(SubsetProperty);
			}
			set
			{
				SetValue(SubsetProperty, value);
			}
		}

		public double MaxX
		{
			get
			{
				return (double)GetValue(MaxXProperty);
			}
			set
			{
				SetValue(MaxXProperty, value);
			}
		}

		public double MinX
		{
			get
			{
				return (double)GetValue(MinXProperty);
			}
			set
			{
				SetValue(MinXProperty, value);
			}
		}

		public double MaxY
		{
			get
			{
				return (double)GetValue(MaxYProperty);
			}
			set
			{
				SetValue(MaxYProperty, value);
			}
		}

		public double MinY
		{
			get
			{
				return (double)GetValue(MinYProperty);
			}
			set
			{
				SetValue(MinYProperty, value);
			}
		}

		public int Interval
		{
			get
			{
				return (int)GetValue(IntervalProperty);
			}
			set
			{
				SetValue(IntervalProperty, value);
			}
		}

		public IList<ChartData> Targets
		{
			get
			{
				return (IList<ChartData>)GetValue(TargetsProperty);
			}
			set
			{
				SetValue(TargetsProperty, value);
			}
		}

		public ESizeMode CurverWidth
		{
			get
			{
				return (ESizeMode)GetValue(CurverWidthProperty);
			}
			set
			{
				SetValue(CurverWidthProperty, value);
			}
		}

		public bool IsDecrypt
		{
			get
			{
				return (bool)GetValue(IsDecryptProperty);
			}
			set
			{
				SetValue(IsDecryptProperty, value);
			}
		}

		public bool ShowCurveToolTips
		{
			get
			{
				return (bool)GetValue(ShowCurveToolTipsProperty);
			}
			set
			{
				SetValue(ShowCurveToolTipsProperty, value);
			}
		}

		public bool DisplayAllSeries { get; set; }

		public bool RefreshAll
		{
			get
			{
				return _RefreshAll;
			}
			set
			{
				_RefreshAll = value;
			}
		}

		public event RoutedEventHandler OnItemUpdate
		{
			add
			{
				AddHandler(OnItemUpdateRoutedEvent, value);
			}
			remove
			{
				RemoveHandler(OnItemUpdateRoutedEvent, value);
			}
		}

		public event RoutedEventHandler OnLineColorChanged
		{
			add
			{
				AddHandler(OnLineColorChangedRoutedEvent, value);
			}
			remove
			{
				RemoveHandler(OnLineColorChangedRoutedEvent, value);
			}
		}

		public event RoutedEventHandler OnThresholdMouseUp
		{
			add
			{
				AddHandler(OnThresholdMouseUpRpoutedEvent, value);
			}
			remove
			{
				RemoveHandler(OnThresholdMouseUpRpoutedEvent, value);
			}
		}

		public void RaiseItemUpdateEvent()
		{
			RaiseEvent(new RoutedEventArgs
			{
				RoutedEvent = OnItemUpdateRoutedEvent
			});
		}

		private static void OnCaptionChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			BaseCurveChart baseCurveChart = sender as BaseCurveChart;
			if (baseCurveChart != null)
			{
				baseCurveChart.UltraChart.Name = baseCurveChart.Caption;
			}
		}

		private static void OnColorModeChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			BaseCurveChart baseCurveChart = sender as BaseCurveChart;
			if (baseCurveChart != null && (!(baseCurveChart is ADCurveChart) || baseCurveChart.ColorMode == EColorMode.Result || baseCurveChart.ColorMode == EColorMode.Type))
			{
				baseCurveChart.UltraChart.CanChangeColor = baseCurveChart.ColorMode != EColorMode.Result;
				baseCurveChart.RefreshChartSeriesColor();
			}
		}

		private static void OnTitleXFormatChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			BaseCurveChart baseCurveChart = sender as BaseCurveChart;
			if (baseCurveChart != null)
			{
				baseCurveChart.UltraChart.SetXFormat(baseCurveChart.TitleXFormat);
			}
		}

		private static void OnTitleYFormatChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			BaseCurveChart baseCurveChart = sender as BaseCurveChart;
			if (baseCurveChart != null)
			{
				baseCurveChart.UltraChart.SetYFormat(baseCurveChart.TitleYFormat);
			}
		}

		private static void OnTitleXChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			BaseCurveChart baseCurveChart = sender as BaseCurveChart;
			if (baseCurveChart != null)
			{
				baseCurveChart.UltraChart.SetTitleX(baseCurveChart.TitleX);
			}
		}

		private static void OnTitleYChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			BaseCurveChart baseCurveChart = sender as BaseCurveChart;
			if (baseCurveChart != null)
			{
				baseCurveChart.UltraChart.SetTitleY(baseCurveChart.TitleY);
			}
		}

		private static void OnThresholdVisibleChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			BaseCurveChart baseCurveChart = sender as BaseCurveChart;
			if (baseCurveChart != null)
			{
				baseCurveChart.RefreshThresholdVisible();
			}
		}

		private static void OnMaxXChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			BaseCurveChart baseCurveChart = sender as BaseCurveChart;
			if (baseCurveChart != null)
			{
				baseCurveChart.SetMaxX(baseCurveChart.MaxX);
			}
		}

		private static void OnMinXChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			BaseCurveChart baseCurveChart = sender as BaseCurveChart;
			if (baseCurveChart != null)
			{
				baseCurveChart.SetMinX(baseCurveChart.MinX);
			}
		}

		private static void OnMaxYChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			BaseCurveChart baseCurveChart = sender as BaseCurveChart;
			if (baseCurveChart != null)
			{
				baseCurveChart.SetMaxY(baseCurveChart.MaxY);
			}
		}

		private static void OnMinYChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			BaseCurveChart baseCurveChart = sender as BaseCurveChart;
			if (baseCurveChart != null)
			{
				baseCurveChart.SetMinY(baseCurveChart.MinY);
			}
		}

		private static void OnTargetsChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			BaseCurveChart baseCurveChart = sender as BaseCurveChart;
			if (baseCurveChart != null)
			{
				baseCurveChart.ClearAllChart();
			}
		}

		private static void OnCurverWidthChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			BaseCurveChart baseCurveChart = sender as BaseCurveChart;
			if (baseCurveChart != null)
			{
				baseCurveChart.ResetCurverWidth();
			}
		}

		private static void OnIsDecryptPropertyChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			BaseCurveChart baseCurveChart = sender as BaseCurveChart;
			if (baseCurveChart != null)
			{
				baseCurveChart.UltraChart.IsDecrypt = baseCurveChart.IsDecrypt;
			}
		}

		private static void OnShowCurveToolTipsChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			BaseCurveChart baseCurveChart = sender as BaseCurveChart;
			if (baseCurveChart != null)
			{
				baseCurveChart.UltraChart.ShowToolTips = baseCurveChart.ShowCurveToolTips;
			}
		}

		public BaseCurveChart()
		{
			RefreshAll = true;
			InitializeComponent();
			UltraChart.ShowToolTips = ShowCurveToolTips;
			UltraChart.IsDecrypt = IsDecrypt;
			ResetCurverWidth();
			RegsiterEvent();
			InitMenu();
		}

		public void Dispose()
		{
			UltraChart.Dispose();
			windowsFormsHost.Dispose();
		}

		private void ResetCurverWidth()
		{
			switch (CurverWidth)
			{
			case ESizeMode.Small:
				UltraChart.LineWidth = 2;
				break;
			case ESizeMode.Middle:
				UltraChart.LineWidth = 3;
				break;
			case ESizeMode.Large:
				UltraChart.LineWidth = 4;
				break;
			}
		}

		private void RegsiterEvent()
		{
			UltraChart.OnSelectedSeriesChanged += UltraChart_OnSelectedSeriesChanged;
			UltraChart.OnGetTooltipsArgs += UltraChart_OnGetTooltipsArgs;
			UltraChart.OnClearHighLightSeries += UltraChart_OnClearHighLightSeries;
			UltraChart.OnChangeCurveStyle += UltraChart_OnChangeCurveStyle;
			UltraChart.OnAxixRangeChanged += UltraChart_OnAxixRangeChanged;
			UltraChart.Chart.MouseMove += UltraChart_MouseMove;
			UltraChart.Chart.MouseDown += UltraChart_MouseDown;
			UltraChart.Chart.MouseUp += UltraChart_MouseUp;
			ConfigSettings.GetInstance().ExperimentSetting.ColorSettings.Event += ColorSettings_Event;
		}

		private void ColorSettings_Event(object sender, EventArgs e)
		{
			if (ColorMode == EColorMode.Channel)
			{
				PropertyChangedEventArgsEx propertyChangedEventArgsEx = e as PropertyChangedEventArgsEx;
				if (propertyChangedEventArgsEx != null && propertyChangedEventArgsEx.PropertyName == "UpdateColor")
				{
					RefreshChartSeriesColor();
				}
			}
		}

		protected virtual void InitMenu()
		{
		}

		protected void RegisterSubsetParamterEvent(ChartData chartData)
		{
			Subset.SubsetParamter subsetParamter = Subset.GetSubsetParamter(chartData.WellCell.Well.Sample.Project);
			if (!htSubsetParamterEvent.Contains(subsetParamter))
			{
				subsetParamter.Event += subsetParamter_Event;
				htSubsetParamterEvent.Add(subsetParamter, true);
			}
		}

		public virtual string GetToolTipFormatX()
		{
			return string.Empty;
		}

		public virtual string GetToolTipFormatY()
		{
			return string.Empty;
		}

		protected virtual string GetToolTipString(ChartData chartData)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.AppendLine(TitleX + Utility.GetMessage("Colon") + "#VAL{X}");
			stringBuilder.AppendLine(TitleY + Utility.GetMessage("Colon") + "#VALX{Y}");
			return stringBuilder.ToString();
		}

		public abstract Curve GetCurve(SampleTargetItem item);

		public virtual Curve GetCurve(ChartData chartData)
		{
			return GetCurve(chartData.SampleTargetItem);
		}

		protected virtual void RefreshThresholdVisible()
		{
			if (ThresholdVisible)
			{
				foreach (ChartData chartData in _DisplayChartData)
				{
					ObservableCollection<BasicOptionItem> items = Subset.GetSubsetParamter(chartData.WellCell.Well.Sample.Project).BasicOption.Items;
					Func<BasicOptionItem, bool> predicate = (BasicOptionItem s) => s.ChannelNo == chartData.SampleTargetItem.ChannelNo && s.TubeNo == chartData.SampleTargetItem.TubeNo;
					CreateThresholdSeries(items.Where(predicate).FirstOrDefault(), chartData);
				}
				{
					foreach (XYSeriesEx thresholdSeries in _ThresholdSeriesList)
					{
						bool visible = false;
						foreach (ChartData chartData2 in _DisplayChartData)
						{
							ObservableCollection<BasicOptionItem> items2 = Subset.GetSubsetParamter(chartData2.WellCell.Well.Sample.Project).BasicOption.Items;
							Func<BasicOptionItem, bool> predicate2 = (BasicOptionItem s) => s.ChannelNo == chartData2.SampleTargetItem.ChannelNo && s.TubeNo == chartData2.SampleTargetItem.TubeNo;
							if (items2.Where(predicate2).FirstOrDefault() == thresholdSeries.Tag)
							{
								visible = true;
								break;
							}
						}
						thresholdSeries.Visible = visible;
					}
					return;
				}
			}
			foreach (XYSeriesEx thresholdSeries2 in _ThresholdSeriesList)
			{
				thresholdSeries2.Visible = false;
			}
		}

		protected virtual void CreateThresholdSeries(BasicOptionItem item, ChartData chartData)
		{
		}

		protected virtual void ResetThresholdSeriesPoint()
		{
		}

		protected virtual void RefreshThresholdValue(BasicOptionItem item, string property)
		{
		}

		protected virtual string GetThresholdToolTips(XYSeriesEx series)
		{
			return string.Empty;
		}

		protected virtual void SetThreshold(XYSeriesEx series, int x, int y)
		{
		}

		protected virtual Cursor GetThresholdDragCursor(string key)
		{
			return Cursors.Default;
		}

		protected virtual bool CanThresholdChanged(BasicOptionItem item)
		{
			return false;
		}

		protected virtual void RefreshAxisTitle()
		{
		}

		public virtual void RefreshSeries(bool refreshData = false)
		{
			if (Targets == null)
			{
				return;
			}
			CreateNewSeries();
			if (refreshData || RefreshAll)
			{
				foreach (ChartData target in Targets)
				{
					if (_DisplaySeries.ContainsKey(target))
					{
						ResetSeriesPoint(_DisplaySeries[target] as XYSeriesEx, target);
					}
				}
				RefreshAll = false;
			}
			_DisplayChartData.Clear();
			foreach (ChartData key in _DisplaySeries.Keys)
			{
				XYSeriesEx xYSeriesEx = _DisplaySeries[key] as XYSeriesEx;
				xYSeriesEx.Visible = GetSeriesVisible(key);
				if (xYSeriesEx.Visible)
				{
					_DisplayChartData.Add(key);
				}
			}
			RefreshThresholdVisible();
			RefreshAxisTitle();
		}

		public void RefleshSeriesPoint()
		{
			ResetThresholdSeriesPoint();
		}

		protected virtual bool GetSeriesVisible(ChartData chartData)
		{
			if (!DisplayAllSeries)
			{
				if (chartData.IsVisible)
				{
					return chartData.WellCell.IsInfoSelected;
				}
				return false;
			}
			return true;
		}

		protected virtual System.Drawing.Color GetChartDataSeriesColor(ChartData chartData)
		{
			return Utility.WindowsColorToDrawingColor(chartData.GetChartDataColor(ColorMode));
		}

		protected virtual ESeriesType GetSymbolIcon(ChartData chartData)
		{
			if (PrintMode)
			{
				if (!ConfigSettings.GetInstance().PrintSetting.ColorPrinter)
				{
					return ConfigSettings.GetInstance().PrintSetting.CurveStyle[chartData.SampleTargetItem.ChannelNo - 1];
				}
			}
			else if (chartData.SampleTargetItem.HasCustomLinePointStyle(ColorMode))
			{
				return chartData.SampleTargetItem.GetCustomLinePointStyle(ColorMode);
			}
			return ESeriesType.None;
		}

		private void UltraChart_MouseDown(object sender, MouseEventArgs e)
		{
			_IsMouseDown = true;
		}

		private void UltraChart_MouseUp(object sender, MouseEventArgs e)
		{
			if (_IsMouseDown)
			{
				_IsMouseDown = false;
				if (_ThresholdSeries != null)
				{
					UltraChart.SetCursor(Cursors.Arrow);
					_ThresholdSeries = null;
					UltraChart.CanScale = true;
					UltraChart.DisplayAnnotation(false);
					RaiseEvent(new RoutedEventArgs
					{
						RoutedEvent = OnThresholdMouseUpRpoutedEvent,
						Source = this
					});
				}
			}
		}

		private void UltraChart_MouseMove(object sender, MouseEventArgs e)
		{
			if (!_IsMouseDown)
			{
				if (UltraChart.SelectedSerie == null)
				{
					UltraChart.SetCursor(Cursors.Arrow);
					_ThresholdSeries = null;
					UltraChart.CanScale = true;
					UltraChart.DisplayAnnotation(false);
				}
			}
			else if (_ThresholdSeries != null)
			{
				try
				{
					SetThreshold(_ThresholdSeries, e.X, e.Y);
					UltraChart.SetAnnotationText(GetThresholdToolTips(_ThresholdSeries));
				}
				catch (ArgumentException)
				{
				}
			}
		}

		private void UltraChart_OnChangeCurveStyle(object sender, SelectedSeriesChangedArgs e)
		{
			if (e.SelectedSeries != null)
			{
				ChartData chartData = (ChartData)((XYSeriesEx)e.SelectedSeries).Tag;
				System.Windows.Media.Color chartDataColor = chartData.GetChartDataColor(ColorMode);
				ESeriesType symbolIcon = GetSymbolIcon(chartData);
				CurveStyleSettingWindow curveStyleSettingWindow = new CurveStyleSettingWindow();
				curveStyleSettingWindow.Tag = chartData;
				curveStyleSettingWindow.Owner = Utility.GetMainWindow();
				curveStyleSettingWindow.Color = chartDataColor;
				curveStyleSettingWindow.Symbol = symbolIcon;
				curveStyleSettingWindow.OnApply += window_OnApply;
				bool flag = curveStyleSettingWindow.ShowDialog() == true;
			}
		}

		private void window_OnApply(object sender, EventArgs e)
		{
			CurveStyleSettingWindow curveStyleSettingWindow = sender as CurveStyleSettingWindow;
			if (curveStyleSettingWindow == null)
			{
				return;
			}
			ChartData chartData = curveStyleSettingWindow.Tag as ChartData;
			if (chartData != null)
			{
				if (curveStyleSettingWindow.Symbol != GetSymbolIcon(chartData))
				{
					chartData.SampleTargetItem.AddLinePointStyle(ColorMode, curveStyleSettingWindow.Symbol);
				}
				if (curveStyleSettingWindow.Color != chartData.GetChartDataColor(ColorMode))
				{
					chartData.SampleTargetItem.AddColor(ColorMode, curveStyleSettingWindow.Color);
				}
				RaiseItemUpdateEvent();
			}
		}

		private void UltraChart_OnAxixRangeChanged(object sender, EventArgs e)
		{
			ResetThresholdSeriesPoint();
		}

		private void UltraChart_OnClearHighLightSeries(object sender, EventArgs e)
		{
			ISeries[] highLightSreis = UltraChart.GetHighLightSreis();
			for (int num = highLightSreis.Count() - 1; num >= 0; num--)
			{
				ChartData chartData = ((XYSeriesEx)highLightSreis[num]).Tag as ChartData;
				chartData.HighLight = false;
				chartData.WellCell.HighLight = false;
			}
		}

		private void UltraChart_OnGetTooltipsArgs(object sender, GetTooltipsArgs e)
		{
			if (_IsMouseDown)
			{
				e.Handle = true;
			}
			else if (e.Series != null && e.Series is XYSeriesEx && ((XYSeriesEx)e.Series).Tag is ChartData)
			{
				ChartData chartData = ((XYSeriesEx)e.Series).Tag as ChartData;
				if (ShowCurveToolTips)
				{
					e.Tooltips = GetToolTipString(chartData);
					e.FromatX = GetToolTipFormatX();
					e.FromatY = GetToolTipFormatY();
				}
			}
		}

		private void UltraChart_OnSelectedSeriesChanged(object sender, SelectedSeriesChangedArgs e)
		{
			if (_IsMouseDown)
			{
				return;
			}
			if (e.UnSelectedSeries != null && HighLight && ((XYSeriesEx)e.UnSelectedSeries).Tag is ChartData)
			{
				ChartData chartData = ((XYSeriesEx)e.UnSelectedSeries).Tag as ChartData;
				chartData.HighLight = false;
				chartData.WellCell.HighLight = false;
			}
			if (e.SelectedSeries == null)
			{
				return;
			}
			if (((XYSeriesEx)e.SelectedSeries).Tag is ChartData)
			{
				if (HighLight)
				{
					ChartData chartData2 = ((XYSeriesEx)e.SelectedSeries).Tag as ChartData;
					chartData2.HighLight = true;
					chartData2.WellCell.HighLight = true;
				}
			}
			else if (((XYSeriesEx)e.SelectedSeries).Tag is BasicOptionItem)
			{
				_ThresholdSeries = (XYSeriesEx)e.SelectedSeries;
				UltraChart.DisplayAnnotation();
				UltraChart.SetAnnotationText(GetThresholdToolTips(_ThresholdSeries));
				if (!CanThresholdChanged((BasicOptionItem)_ThresholdSeries.Tag))
				{
					_ThresholdSeries = null;
					return;
				}
				UltraChart.SetCursor(GetThresholdDragCursor(((XYSeriesEx)e.SelectedSeries).Key));
				UltraChart.CanScale = false;
			}
		}

		private void subsetParamter_Event(object sender, EventArgs e)
		{
			PropertyChangedEventArgsEx propertyChangedEventArgsEx = e as PropertyChangedEventArgsEx;
			if (propertyChangedEventArgsEx != null && propertyChangedEventArgsEx.PropertyName == "BasicOption")
			{
				UltraChart.ClearAllThresholdSeries();
				_ThresholdSeriesList.Clear();
				RefreshThresholdVisible();
			}
		}

		protected void BasicOptionItem_Event(object sender, EventArgs e)
		{
			PropertyChangedEventArgsEx propertyChangedEventArgsEx = e as PropertyChangedEventArgsEx;
			if (propertyChangedEventArgsEx != null)
			{
				RefreshThresholdValue((BasicOptionItem)sender, propertyChangedEventArgsEx.PropertyName);
			}
		}

		protected void ClearAllChart()
		{
			UltraChart.ClearAllSeries();
			_ThresholdSeriesList.Clear();
			_DisplaySeries.Clear();
		}

		private void CreateNewSeries()
		{
			List<ChartData> list = Targets.OrderByDescending((ChartData s) => s.WellCell.IsInfoSelected).ToList();
			foreach (ChartData item2 in list)
			{
				if (!_DisplaySeries.Contains(item2))
				{
					CreateSeries(item2);
				}
			}
			List<ChartData> list2 = new List<ChartData>();
			foreach (ChartData key in _DisplaySeries.Keys)
			{
				if (!Targets.Contains(key))
				{
					list2.Add(key);
				}
			}
			foreach (ChartData item3 in list2)
			{
				UltraChart.RemoveDataSeries(_DisplaySeries[item3] as XYSeriesEx);
				_DisplaySeries.Remove(item3);
			}
		}

		private void chartData_Event(object sender, EventArgs e)
		{
			PropertyChangedEventArgsEx propertyChangedEventArgsEx = e as PropertyChangedEventArgsEx;
			if (propertyChangedEventArgsEx != null && HighLight && sender is ChartData && propertyChangedEventArgsEx.PropertyName == "HighLight")
			{
				ChartData chartData = sender as ChartData;
				XYSeriesEx xYSeriesEx = (XYSeriesEx)_DisplaySeries[chartData];
				if (xYSeriesEx != null)
				{
					xYSeriesEx.PEs[0].Fill = (chartData.HighLight ? System.Drawing.Color.Black : GetChartDataSeriesColor(xYSeriesEx.Tag as ChartData));
					UltraChart.AddHighLightSeries(xYSeriesEx, chartData.HighLight);
				}
			}
		}

		private void SampleTargetItem_Event(object sender, EventArgs e)
		{
			PropertyChangedEventArgsEx propertyChangedEventArgsEx = e as PropertyChangedEventArgsEx;
			if (propertyChangedEventArgsEx == null)
			{
				return;
			}
			if (propertyChangedEventArgsEx.PropertyName == "LineColor")
			{
				ChartData chartData = (from ChartData s in _DisplaySeries.Keys
					where s.SampleTargetItem == sender
					select s).FirstOrDefault();
				if (chartData != null)
				{
					XYSeriesEx xYSeriesEx = _DisplaySeries[chartData] as XYSeriesEx;
					UltraChart.ChangeSeriesIcon(xYSeriesEx, GetSymbolIcon(chartData));
					xYSeriesEx.PEs[0].Fill = GetChartDataSeriesColor(chartData);
					if (xYSeriesEx.Visible)
					{
						xYSeriesEx.Visible = false;
						xYSeriesEx.Visible = true;
					}
					RaiseEvent(new RoutedEventArgs
					{
						RoutedEvent = OnLineColorChangedRoutedEvent
					});
				}
			}
			else if (propertyChangedEventArgsEx.PropertyName == "LinePointStyle")
			{
				ChartData chartData2 = (from ChartData s in _DisplaySeries.Keys
					where s.SampleTargetItem == sender
					select s).FirstOrDefault();
				if (chartData2 != null)
				{
					XYSeriesEx series = _DisplaySeries[chartData2] as XYSeriesEx;
					UltraChart.ChangeSeriesIcon(series, GetSymbolIcon(chartData2));
					RaiseEvent(new RoutedEventArgs
					{
						RoutedEvent = OnLineColorChangedRoutedEvent
					});
				}
			}
		}

		private void CreateSeries(ChartData chartData)
		{
			chartData.Event += chartData_Event;
			chartData.SampleTargetItem.Event += SampleTargetItem_Event;
			XYSeriesEx xYSeriesEx = new XYSeriesEx();
			xYSeriesEx.Tag = chartData;
			xYSeriesEx.Well = chartData.WellCell.Well.CellName;
			xYSeriesEx.Tube = chartData.SampleTargetItem.TubeName;
			xYSeriesEx.Channel = chartData.SampleTargetItem.ChannelNo;
			xYSeriesEx.PEs.Add(new PaintElement());
			xYSeriesEx.Visible = false;
			ResetSeriesPoint(xYSeriesEx, chartData);
			UltraChart.AddDataSeries(xYSeriesEx, GetSymbolIcon(chartData));
			_DisplaySeries.Add(chartData, xYSeriesEx);
		}

		private void ResetSeriesPoint(XYSeriesEx series, ChartData chartData)
		{
			Curve curve = GetCurve(chartData);
			if (curve == null)
			{
				series.Points.Clear();
				return;
			}
			for (int i = 0; i < curve.CurvePoint.Count; i++)
			{
				Dot dot = curve.CurvePoint[i];
				double valueY = dot.Y;
				if (!UltraChart.IsAxisYLinear() && dot.Y < UltraChart.MinY)
				{
					valueY = UltraChart.LogMinY / 10.0;
				}
				if (i < series.Points.Count)
				{
					series.Points[i].ValueX = dot.X;
					series.Points[i].ValueY = valueY;
				}
				else
				{
					series.Points.Add(new XYDataPoint(dot.X, valueY, "", false));
				}
			}
			for (int num = series.Points.Count - 1; num >= curve.CurvePoint.Count; num--)
			{
				series.Points.RemoveAt(num);
			}
			series.PEs[0].Fill = GetChartDataSeriesColor(chartData);
		}

		protected void RefreshChartSeriesColor()
		{
			foreach (ChartData key in _DisplaySeries.Keys)
			{
				XYSeriesEx xYSeriesEx = _DisplaySeries[key] as XYSeriesEx;
				xYSeriesEx.PEs[0].Fill = GetChartDataSeriesColor(key);
				UltraChart.ChangeSeriesIcon(xYSeriesEx, GetSymbolIcon(key));
				if (xYSeriesEx.Visible)
				{
					xYSeriesEx.Visible = false;
					xYSeriesEx.Visible = true;
				}
			}
		}

		protected System.Drawing.Color GetThresholdSeriesColor(int channelNo)
		{
			switch (channelNo)
			{
			case 1:
				return Utility.WindowsColorToDrawingColor((System.Windows.Media.Color)Utility.GetResource("ColorThresholdChannel1"));
			case 2:
				return Utility.WindowsColorToDrawingColor((System.Windows.Media.Color)Utility.GetResource("ColorThresholdChannel2"));
			case 3:
				return Utility.WindowsColorToDrawingColor((System.Windows.Media.Color)Utility.GetResource("ColorThresholdChannel3"));
			case 4:
				return Utility.WindowsColorToDrawingColor((System.Windows.Media.Color)Utility.GetResource("ColorThresholdChannel4"));
			case 5:
				return Utility.WindowsColorToDrawingColor((System.Windows.Media.Color)Utility.GetResource("ColorThresholdChannel5"));
			case 6:
				return Utility.WindowsColorToDrawingColor((System.Windows.Media.Color)Utility.GetResource("ColorThresholdChannel6"));
			default:
				return System.Drawing.Color.Black;
			}
		}

		protected virtual void SetMaxX(double value)
		{
			UltraChart.MaxX = value;
		}

		protected virtual void SetMinX(double value)
		{
			UltraChart.MinX = value;
		}

		protected virtual void SetMaxY(double value)
		{
			digitLen = 6 - value.ToString("0").Length;
			digitLen = ((digitLen >= 0) ? digitLen : 0);
			StringBuilder stringBuilder = new StringBuilder("");
			for (int i = 0; i < 1; i++)
			{
				stringBuilder.Append("0");
			}
			stringBuilder.Append(".");
			for (int j = 0; j < digitLen; j++)
			{
				stringBuilder.Append("0");
			}
			TitleYFormat = stringBuilder.ToString();
			UltraChart.LogMinY = Math.Pow(10.0, -digitLen);
			if (!UltraChart.IsAxisYLinear())
			{
				value = Math.Pow(10.0, 6 - digitLen);
			}
			UltraChart.MaxY = value;
		}

		protected virtual void SetMinY(double value)
		{
			if (!UltraChart.IsAxisYLinear())
			{
				value = UltraChart.LogMinY;
			}
			UltraChart.MinY = value;
		}
	}
}
